<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="入职时间" prop="incomeDate">
        <!-- <el-date-picker
          v-model="form.leaveDate"
          format="yyyy-MM-dd HH:mm"
          value-format="yyyy-MM-dd HH:mm"
          type="datetimerange"
          range-separator="至"
          start-placeholder="请假开始日期"
          end-placeholder="请假结束日期"
          @input="change($event)">
        </el-date-picker> -->
        <el-date-picker
          v-model="form.incomeDate"
          type="date"
          size="small"
          value-format="yyyy-MM-dd"
          @change="change($event)"
          placeholder="入职时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="转正时间" prop="becomeDate">
        <el-date-picker
          v-model="form.becomeDate"
          type="date"
          size="small"
          value-format="yyyy-MM-dd"
          @change="change($event)"
          placeholder="转正时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="请假类型" prop="type">
        <el-select v-model="form.type" placeholder="请输入请假类型" style="width: 100%">
          <template v-for="item in leaveTypeOptionList">
            <el-option :value="item.dictValue" :label="item.dictLabel"/>
          </template>
        </el-select>
      </el-form-item> -->
      <el-form-item label="自我评价" prop="reason">
        <el-input v-model="form.reason" type="textarea" :rows="3" placeholder="请输入自我评价" @input="change($event)"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSave('form')">保存</el-button>
        <el-button type="primary" @click="handleSubmit('form')">保存并提交</el-button>
        <el-button @click="handleReset('form')">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import TablePage from "@/components/CommonForm/table-page";
import {createAndSubmitBecome, saveBecome} from "_a/office/become/become";
import {mapActions} from "vuex";

export default {
  name: 'become-apply',
  components: {TablePage},
  data() {
    return {
      form: {},
      rules: {
        incomeDate: [this.$ruler('入职时间')],
        becomeDate: [this.$ruler('转正时间')],
        type: [this.$ruler('请假类型')],
        reason: [this.$ruler('请假事由')],
      }

    }
  },
  watch: {
    'form.incomeDate': function (newVal, oldVal) {
      if (newVal !== undefined && newVal !== null) {
        this.form.incomeDate = newVal
      }
    },
    'form.becomeDate': function (newVal, oldVal) {
      if (newVal !== undefined && newVal !== null) {
        this.form.becomeDate = newVal
      }
    }
  },
  methods: {
    ...mapActions(['getDictListByType']),
    change(e) {
      this.$forceUpdate()
    },
    /**
     * 重置表单
     * @param name
     */
    handleReset(name) {
      this.dialogShow = false;
      this.$refs[name].resetFields()
    },
    handleSave(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          delete this.form.incomeDate
          delete this.form.becomeDate
          saveBecome(this.form).then(res => {
            if (res.data) {
              this.$message.success("保存成功")
            } else {
              this.$message.error("保存失败")
            }
            // 清空表单
            this.handleReset('form');
          })
        }
      })
    },
    /**
     * 提交表单
     * @param name
     */
    handleSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          console.log("submit form data：",this.form)
          createAndSubmitBecome(this.form).then(res => {
            if (res.data) {
              this.$message.success("提交成功")
            } else {
              this.$message.error("提交失败")
            }
            // 清空表单
            this.handleReset('form');
          })
        }
      })
    },
  }
}
</script>

<style scoped lang="scss">

</style>
